home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: qsort help
- Date: Wed, 03 Jan 96 02:34:02 GMT
- Organization: none
- Distribution: world
- Message-ID: <820636442snz@genesis.demon.co.uk>
- References: <4ccio7$7c0@charm.magnus.acs.ohio-state.edu>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4ccio7$7c0@charm.magnus.acs.ohio-state.edu>
- xiaoyi@bmecg.bme.ohio-state.edu "Xiaoyi Wu" writes:
-
- >Hi, I am having some problems with the qsort routine.
- >
- >
- >int (*compar)(int *a, int *b)
-
- This declares a pointer to a function. A function pointer can't have a
- function body.
-
- >{
- > if (*a < *b) return -1;
- > else if (*a == *b) return 0;
- > else return 1;
- >}
-
- The prototype for qsort is:
-
- void qsort(void *base, size_t nmemb, size_t size,
- int (*compar)(const void *, const void *));
-
- This means that it takes a pointer to a comparison function that has the
- following prototype:
-
- int compar(const void *, const void *);
-
- The parameters of the function *must* be const void *, they can't be
- anything else. So your comparison function could look something like:
-
- int compar(const void *v1, const void *v2)
- {
- const int *p1 = v1;
- const int *p2 = v2;
-
- if (*p1 < *p2) return -1;
- else if (*p1 == *p2) return 0;
- else return 1;
- }
-
- void * pointers are assignment compatible with other types of pointer
- (except function pointers) but they are not guaranteed to have the
- same internal representation as other pointer types so you can't
- legally pass a void * pointer to a function expecting a pointer to
- a different type. The assignment gives the compiler a chance to perform
- any necessary internal format conversions. If no conversions are
- necessary the chances are that your compiler will optimise the assignments
- out completely.
-
- >int return_moved_contour(int ix, int iy)
-
- ...
-
-
- There are a lot of undeclared variabled in this function - please try to
- post code that will compile (as far as possible - obviously that may not be
- the case where the question is about a compilation error).
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-